from SimPy.Simulation import *
from threading import Thread

class obj(Process):
	def __init__(self, val, t):
		Process.__init__(self)
		self.val = val
		self.t = t
	
	def run(self):
		while True:
			print self.val
			yield hold, self, self.t
	

class X(Thread):
	def __init__(self, vals, ts):
		Thread.__init__(self)
		self.vals = vals
		self.ts = ts
		self.active = False
		self.finalize = False
		self.reset()
		
	def reset(self):
		initialize()
		i = 0
		self.a = obj(self.vals[i], self.ts[i])
		i += 1
		self.b = obj(self.vals[i], self.ts[i])
		activate(self.a, self.a.run())
		activate(self.b, self.b.run())
	
	def run(self):
		self.active = True
		while True:
		#self.active = True
			if self.active:
				simulate(until = 10)
			if self.finalize:
				return
	
	def stop(self):
		import time
		time.sleep(1)
		self.active = False
		stopSimulation()
	
	def restart(self):
		self.active = True
	
x = X(["aaaa", "bb"], [0.5, 1.5])
x.start()
x.stop()
print "=============="
x.vals = [1111,22222]
x.reset()
x.restart()
x.stop()
print "==============2"
x.restart()
x.finalize = True

	
	